package com.petiot.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.ViewGroup;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.json.JSONObject;

import com.petiot.rabbitmq.PushService;
import com.petiot.rabbitmq.R;
import com.petiot.rabbitmq.WavIoTClient;
import com.petiot.rabbitmq.WavIoTDevice;
import com.petiot.rabbitmq.PushService.PushMsgCallback;
import com.petiot.ui.fragment.BaseFragment;


/**
 * 空白页面
 */
public class Home1Fra extends BaseFragment implements OnClickListener {

    public static String FRAGMENT_TAG = Home1Fra.class.getSimpleName();
	private Button button1;
	private Button button2;
	private Button button3;
	private Button button4;
	private static final int ThreadPoolSize = 1;
	private ScheduledExecutorService executorService;
	
	String group_id = "A0000";
	String myclient_uid = "ZHUWEI_WAVIOT_VDEVICE_20151111_1";
	
	public WavIoTClient waviotclient = new WavIoTClient(group_id, myclient_uid);
	private PushMsgCallback callBack;
	public WavIoTDevice waviotdevice = new WavIoTDevice(group_id, myclient_uid, "1001", "111", waviotclient.getClientQueueName(), callBack);
	private static Handler handler;
	
    @Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		final View v = inflater.inflate(R.layout.index_1, null);
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				String message = msg.getData().getString("msg");
				TextView tv = (TextView)v.findViewById(R.id.textView);
				Date now = new Date();
				SimpleDateFormat ft = new SimpleDateFormat("HH:mm:ss");
				tv.append(ft.format(now) + ' ' + message + '\n');
				//Toast.makeText(this, message, Toast.LENGTH_LONG).show();
			}
		};
		button1 = (Button)v.findViewById(R.id.obverse);
		button2 = (Button)v.findViewById(R.id.weight);
		button3 = (Button)v.findViewById(R.id.humiture);
		button4 = (Button)v.findViewById(R.id.stop);
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		button3.setOnClickListener(this);
		button4.setOnClickListener(this);

		return v;
	}

    Thread connectionThread ;
	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.obverse:
			waviotdevice.doFeeding("on", waviotclient.getChannel(), myclient_uid);
			break;
		case R.id.weight:
			waviotdevice.doFeeding("getweight", waviotclient.getChannel(), myclient_uid);
			break;
		case R.id.humiture:
			waviotdevice.doFeeding("gethumiture", waviotclient.getChannel(), myclient_uid);
			break;
		case R.id.stop:
			waviotdevice.doFeeding("off", waviotclient.getChannel(), myclient_uid);
			break;

		default:
			break;
		}
	}

	public void callBack(String result) {
		// TODO Auto-generated method stub
		Log.e(FRAGMENT_TAG, "result = " + result);
		Message msg = handler.obtainMessage();
		Bundle bundle = new Bundle();

		bundle.putString("msg", result);
		msg.setData(bundle);
		handler.sendMessage(msg);	
	}
	
	public void onDestroy() {
		super.onDestroy();
		connectionThread.interrupt();
		//unregisterReceiver(receiver);
	};
}
